﻿using HDMCode.FrameEditor;
using System.Diagnostics;
using System.IO;
using HDMCode.Hotfix;
using UnityEditor;
using UnityEngine;
using Debug = UnityEngine.Debug;

namespace HDMCode.ExcelEditor
{
    public class HDMFrameExcelPage
    {
        /// <summary>
        /// 资源打包配置文件，项目相对路径
        /// </summary>
        public static readonly string PackedProjectPath = $"{System.Environment.CurrentDirectory}/Packages/com.hdmframe.excel/LubanFile/~Luaban.zip";

        /// <summary>
        /// luban配置模板文件，项目相对路径
        /// </summary>
        public static readonly string PackedProjectLubanPath = $"{System.Environment.CurrentDirectory}/Packages/com.hdmframe.excel/LubanFile/__tables__.xlsx";

        /// <summary>
        /// 资源打包配置文件，资源相对路径
        /// </summary>
        public static readonly string PackedAssetsPath = $"Packages/com.hdmframe.excel/Setting";

        [HDMCode.FrameEditor.HDMEditorMenu("核心工具", "配置表", "Execl.png")]

        public static void DrawExcel() 
        {
            HDMEditorHelper.DrawSettingIcon(() => 
            {
               Application.OpenURL("https://luban.doc.code-philosophy.com/");
            });
            
            DrawExcelSetting _drawExeclSetting = AssetDatabase.LoadAssetAtPath<DrawExcelSetting>($"{PackedAssetsPath}/ExeclSetting.asset");

            string outPut = $"{System.Environment.CurrentDirectory}/luban";
            if (Directory.Exists(outPut))
            {
                HDMEditorHelper.DrawTileLable("配置表设置");

                GUILayout.Space(10);
                
                using (new EditorGUILayout.HorizontalScope())
                {
                    GUILayout.Label("配置表放置路径", HDMEditorStyle.styleContent, new[] { GUILayout.Width(120), GUILayout.Height(20) });
                    EditorGUILayout.HelpBox(_drawExeclSetting.ExeclPath, MessageType.None);
                    if (GUILayout.Button("选择", GUILayout.Width(50)))
                    {
                        _drawExeclSetting.ExeclPath = EditorUtility.OpenFolderPanel("路径", "", "");
                    }
                }
                
                GUILayout.Space(10);
                
                using (new EditorGUILayout.HorizontalScope())
                {
                    GUILayout.Label("客户端输出路径", HDMEditorStyle.styleContent, new[] { GUILayout.Width(120), GUILayout.Height(20) });
                    EditorGUILayout.HelpBox(_drawExeclSetting.OutPutClientPath, MessageType.None);
                    if (GUILayout.Button("选择", GUILayout.Width(50)))
                    {
                        _drawExeclSetting.OutPutClientPath = EditorUtility.OpenFolderPanel("路径", "", "");
                    }
                }
                
                GUILayout.Space(10);
                
                using (new EditorGUILayout.HorizontalScope())
                {
                    GUILayout.Label("服务器输出路径", HDMEditorStyle.styleContent, new[] { GUILayout.Width(120), GUILayout.Height(20) });
                    EditorGUILayout.HelpBox(_drawExeclSetting.OutPutServerPath, MessageType.None);
                    if (GUILayout.Button("选择", GUILayout.Width(50)))
                    {
                        _drawExeclSetting.OutPutServerPath = EditorUtility.OpenFolderPanel("路径", "", "");
                    }
                }
                
                GUILayout.Space(20);
                
                HDMEditorHelper.DrawButton("打开配置文件","", () =>
                {
                    DrawDeployAssembly.OpenEXETool(DrawDeployAssembly.LubanConfigPath);
                });
                
                if (GUI.changed)
                    EditorUtility.SetDirty(_drawExeclSetting);

                HDMCode.FrameEditor.HDMEditorHelper.DrawPagaTipLable("正在使用Luban环境包");
            }
            else
            {
                try
                {
                    Process process = new Process();
                    process.StartInfo.FileName = "dotnet";
                    process.StartInfo.Arguments = "--list-sdks";
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.CreateNoWindow = true;
                    process.StartInfo.Verb = "RunAs";
                    process.Start();
                    string print = process.StandardOutput.ReadToEnd();
                    process.WaitForExit();

                    UnityEngine.Debug.Log(print);

                    CheckDotent(print, outPut);
                }
                catch (System.Exception)
                {
                    DirectoryInfo directoryInfo = new DirectoryInfo("C:/Program Files/dotnet/sdk");
                    foreach (var item in directoryInfo.GetDirectories())
                    {
                        CheckDotent(item.FullName, outPut);
                    }
                }
            }
        }

        public static void CheckDotent(string print,string outPut) 
        {
            if (print.Contains("8.0"))
            {
                HDMCode.FrameEditor.HDMEditorHelper.DrawButton("使用并安装Luban环境包", "", buttonEvent: () =>
                {
                    if (!Directory.Exists(outPut))
                        Directory.CreateDirectory(outPut);
                    HDMCode.FrameEditor.UnZipStream.Unzip(PackedProjectPath, outPut);
                });

            }
            else
            {
                HDMCode.FrameEditor.HDMEditorHelper.DrawButton("当前系统并未安装 .Net SDK 8 环境包，前往安装", "", buttonEvent: () =>
                {
                    UnityEngine.Application.OpenURL("https://dotnet.microsoft.com/zh-cn/download/dotnet/8.0");
                });
            }
        }

    }
}

